package Long.Lu.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @ClassName:
 * @description:
 * @author: yh
 * @createDate: 2020/10/26
 * @version: 1.0
 */
/**/
@Configuration
public class ShiroConfig {
    /**
     * 配置密码匹配器
     *
     * @return
     */
    @Bean(name = {"hashedCred"})
    public HashedCredentialsMatcher getMatcher() {
        HashedCredentialsMatcher hsc = new HashedCredentialsMatcher();
        //加密算法
        hsc.setHashAlgorithmName("MD5");
        //加密次数
        hsc.setHashIterations(1024);
        return hsc;
    }

    /**
     * 配置自定义的 realm
     *
     * @param hs
     * @return
     */
    @Bean(name = {"myRealm"})
    public MyRealm getRealm(@Qualifier("hashedCred") HashedCredentialsMatcher hs) {
        MyRealm realm = new MyRealm();
        realm.setCredentialsMatcher(hs);
        return realm;
    }

    /**
     * 配置安全管理类
     *
     * @return
     */
    @Bean(name = {"securityManager"})
    public DefaultWebSecurityManager getSecurity(@Qualifier("myRealm") MyRealm realm) {
        DefaultWebSecurityManager dm = new DefaultWebSecurityManager();
        dm.setRealm(realm);
        return dm;
    }

    @Bean
    public LifecycleBeanPostProcessor getProcess() {
        return new LifecycleBeanPostProcessor();
    }

    //html 标签配置
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

    /**
     * 配置权限
     *
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilter(@Qualifier("securityManager") DefaultWebSecurityManager sm) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(sm);
        //配置登录页面
        bean.setLoginUrl("/login");
        //配置权限页面
        bean.setUnauthorizedUrl("/noPower");
        //配置权限的map
        HashMap<String, String> map = new HashMap<String, String>();

        //必须登录才能访问
       map.put("/index", "authc");
        //设置访问用户管理业页面需要userInforList权限
       // map.put("/userInfo/list", "perms[userInforList]");
        //*3.配置拥有vip角色的可以访问会员删除页面*//*
       /// map.put("/memberDel", "roles[vip]");
        bean.setFilterChainDefinitionMap(map);

        return bean;
    }
}
